package com.wis.bolt.risk;

import com.wis.TopologyDrive;
import org.apache.commons.lang.StringUtils;
import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values;

import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * @Description: traffic_wis
 * @author: fan
 * @Date: Created in 2018/12/19 9:12
 * @Modified By:
 */
public class ZongHeZuHeBolt extends BaseRichBolt {
    private OutputCollector collector;
    private static String typeProduce;
    private Queue tupleQueue = new ConcurrentLinkedQueue();
    private Queue tupleIdQueue = new ConcurrentLinkedQueue();

    @Override
    public void prepare(Map map, TopologyContext context, OutputCollector collector) {
        this.collector = collector;
        typeProduce = map.get(TopologyDrive.TYPE_PRODUCE).toString();
    }

    @Override
    public void execute(Tuple tuple) {
        String key = tuple.getStringByField("key");//"key", "id", "nengjiandu", "fengsu", "jiangshuiliang", "jiangshuixiangtai", "qiwen", "xiangduishidu", "daolujiebing"
        String njd = tuple.getStringByField("nengjiandu");

        try {
            if (StringUtils.isNotBlank(key)) {
                if (njd.equals("over")) {
                    if (tupleQueue.size() > 0)
                        this.collector.emit(new Values(key, StringUtils.join(tupleIdQueue, ","), StringUtils.join(tupleQueue, ",")));
                    tupleQueue.clear();
                    tupleIdQueue.clear();
                } else if (njd.equals("jies")) {
                    if (tupleQueue.size() > 0)
                        this.collector.emit(new Values(key, StringUtils.join(tupleIdQueue, ","), StringUtils.join(tupleQueue, ",")));
                    this.collector.emit(new Values(key, "over", "over"));
                    tupleQueue.clear();
                    tupleIdQueue.clear();
                } else {
                    String[] nengjiandu = njd.split(",", -1);
                    String[] fengsu = tuple.getStringByField("fengsu").split(",", -1);
                    String[] jiangshuiliang = tuple.getStringByField("jiangshuiliang").split(",", -1);
                    // String[] jiangshuixiangtai = tuple.getStringByField("jiangshuixiangtai").split(",", -1);
                    String[] qiwen = tuple.getStringByField("qiwen").split(",", -1);
                    // String[] xiangduishidu = tuple.getStringByField("xiangduishidu").split(",", -1);
                    String[] daolujiebing = tuple.getStringByField("daolujiebing").split(",", -1);
                    StringBuffer stringBuffer = new StringBuffer();
                    for (int i = 0; i < nengjiandu.length; i++) {
                        if (typeProduce.equals("3") && i == 0 && Integer.parseInt(key.charAt(20) + "") == 2) continue;
                        ArrayList<Integer> arrs = new ArrayList<>();
                        // 将每一个时间点的最大值直接取出来
                        arrs.add(Integer.parseInt(nengjiandu[i]));
                        arrs.add(Integer.parseInt(fengsu[i]));
                        arrs.add(Integer.parseInt(jiangshuiliang[i]));
                        arrs.add(Integer.parseInt(qiwen[i]));
                        arrs.add(Integer.parseInt(daolujiebing[i]));
                        Collections.sort(arrs);
                        Integer maxValue = arrs.get(arrs.size() - 1);
                        stringBuffer.append(maxValue);
                        stringBuffer.append("_");
                    }
                    tupleQueue.add(stringBuffer.toString());
                    tupleIdQueue.add(tuple.getStringByField("id"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.collector.ack(tuple);
        }
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        declarer.declare(new Fields("key", "id", "value"));
    }
}
